package org.crystal.tradecraft.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.crystal.tradecraft.model.binance.market.Order;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BinanceOrdersDeserializer extends JsonDeserializer<List<Order>> {

    @Override
    public List<Order> deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
        JsonNode jsonNode = jsonParser.readValueAsTree();
        List<Order> orders = new ArrayList<>(jsonNode.size());

        for (JsonNode node : jsonNode) {
            orders.add(Order.builder()
                .price(node.get(0).asDouble())
                .qty(node.get(1).asDouble())
                .build());
        }
        return orders;
    }
}
